package cn.pfund.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import cn.pfund.Constants;
import cn.pfund.entity.Product;
import cn.pfund.entity.ProductType;
import cn.pfund.service.ProductService;
import cn.pfund.dao.ProductDao;
import cn.pfund.util.Pagination;
import cn.pfund.util.ParamBean;
import cn.pfund.util.QueryCondition;

@Service("productService")
public class ProductServiceImpl extends BaseServiceImpl implements ProductService {
	@Resource(name="productDao")
    private ProductDao productDao;
    
    @Transactional(readOnly = true)
	public Pagination<Product> getPaginationAdmin(ParamBean paramBean) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		if (StringUtils.isNotBlank(paramBean.getKeyword())) {
			queryConditions.add(new QueryCondition(paramBean.getField(), QueryCondition.LK, paramBean.getKeyword()));
		}
		String orderBy = "order by id desc";
		Pagination<Product> pagination = (Pagination<Product>)getPagination(Product.class, queryConditions, orderBy, paramBean.getPage(), paramBean.getPageSize());
		pagination.setPageHtml(paramBean.getUrl());
		return pagination;
	}
    
    @Transactional(readOnly = true)
	public Pagination<Product> getPagination(ParamBean paramBean) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		queryConditions.add(new QueryCondition("isMarketable", QueryCondition.EQ, true));
		if (paramBean.getProductTypeId() != null && paramBean.getProductTypeId().intValue() > 0) {
			ProductType productType = getById(ProductType.class, paramBean.getProductTypeId());
			queryConditions.add(new QueryCondition("productType", QueryCondition.EQ, productType));
		}
		String orderBy = "order by id desc";
		Pagination<Product> pagination = (Pagination<Product>)getPagination(Product.class, queryConditions, orderBy, paramBean.getPage(), paramBean.getPageSize());
		pagination.setPageHtml(paramBean.getUrl());
		return pagination;
	}

	public void build(Product product) {
		MultipartFile multipartFile = product.getFile();
		if (multipartFile != null && !multipartFile.isEmpty()) {
			try {
				String uploadPath = Constants.UPLOAD_PATH_PREFIX_PRODUCT + DateFormatUtils.format(new Date(), "yyyyMMdd")+"/";
				String uuid = UUID.randomUUID().toString();
				String sourcePath = uploadPath + uuid + FilenameUtils.EXTENSION_SEPARATOR + FilenameUtils.getExtension(multipartFile.getOriginalFilename());

				File sourceFile = new File(sourcePath);
				if (!sourceFile.getParentFile().exists()) {
					sourceFile.getParentFile().mkdirs();
				}
				multipartFile.transferTo(sourceFile);
				product.setSource(sourcePath);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Transactional(readOnly = true)
	public List<Product> getAllProduct(ParamBean paramBean) {
		StringBuilder jpql = new StringBuilder("select distinct p from Product p left join p.tags t where 1=1 ");
		if (paramBean.getProductTypeId() != null && paramBean.getProductTypeId().intValue() > 0) {
			jpql.append("and p.productType.id=").append(paramBean.getProductTypeId());
		}
		if (paramBean.getTagId() != null && paramBean.getTagId().intValue() > 0) {
			jpql.append("and t.id=").append(paramBean.getTagId());
		}
		return getByJpql(jpql.toString());
	}

	@Transactional(readOnly = true)
	public List<Product> getAllProduct(Long productTypeId) {
		ParamBean paramBean = new ParamBean();
		paramBean.setProductTypeId(productTypeId);
		return getAllProduct(paramBean);
	}
}
